<!DOCTYPE html>
<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml" xmlns:v-on="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="vue.js"></script>
    <style>
        .box {
            width: 100%;
            height: 100px;
            border-bottom: 1px solid lightgray;
        }
    </style>
</head>
<body>
<!--声明式渲染-->
<div id="app" class="box">
    {{message}}
</div>

<!--响应式-->
<div id="app-2" class="box">
    <span v-bind:title="message">
         鼠标悬停几秒钟查看此处动态绑定的提示信息！
    </span>
</div>

<!--条件与循环-->
<div id="app-3" class="box">
    <p v-if="seen">看到</p>
</div>

<!--项目列表-->
<div id="app-4" class="box">
    <ol>
        <li v-for="todo in todos">
            {{todo.text}}
        </li>
    </ol>
</div>

<!--监听-->
<div id="app-5" class="box">
    <p>{{message}}</p>
    <button v-on:click="reverseMessage">逆转消息</button>
</div>

<!--双向绑定-->
<div id="app-6" class="box">
    <p>{{message}}</p>
    <input v-model="message">
</div>

<!--组件化-->
<div id="app-7" class="box">
    <ol>
        <todo-item v-for="item in groceryList"
                   v-bind:todo="item"
                   v-bind:key="item.id">
        </todo-item>
    </ol>
</div>

<!--计算属性-->
<div id="example" class="box">
    <p>{{message}}</p>
    <p>{{reversedMessage}}</p>
    <p>{{now}}</p>
    <!--只有在刷新的时候才更新（依赖缓存）-->
</div>

<!--计算属性的setter-->
<div id="demo">{{ fullName }}</div>

<!--watch侦听器-->
<div id="watch-example" class="box">
    <p>
        Ask a yes/no question:
        <input v-model="question">
    </p>
    <p>{{ answer }}</p>
</div>

<script src="https://cdn.jsdelivr.net/npm/axios@0.12.0/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
<script>
    Vue.component('todo-item', {
        props: ['todo'],
        template: `<li>{{todo.text}}</li>`
    });

    let app7 = new Vue({
        el: '#app-7',
        data: {
            groceryList: [
                {id: 0, text: '蔬菜'},
                {id: 1, text: '奶酪'},
                {id: 2, text: '随便其他什么人吃的东西'}
            ]
        }
    });

    let app = new Vue({
        el: '#app',
        data: {
            message: 'Hello Vue!'
        }
    });

    let app2 = new Vue({
        el: '#app-2',
        data: {
            message: '页面加载于' + new Date().toLocaleString()
        }
    });

    let app3 = new Vue({
        el: '#app-3',
        data: {
            seen: true
        }
    });

    let app4 = new Vue({
        el: '#app-4',
        data: {
            todos: [
                {text: '1'},
                {text: '2'},
                {text: '3'}
            ]
        }
    });

    let app5 = new Vue({
        el: '#app-5',
        data: {
            message: 'Hello Vue!'
        },
        methods: {
            reverseMessage: function () {
                this.message = this.message.split('').reverse().join('')
            }
        }
    });

    let app6 = new Vue({
        el: '#app-6',
        data: {
            message: 'Hello Vue!'
        }
    });

    let vm1 = new Vue({
        el: '#example',
        data: {
            message: 'Hello'
        },
        computed: {
            // 计算属性的 getter
            reversedMessage: function () {
                // `this` 指向 vm 实例
                return this.message.split('').reverse().join('')
            },
            now: function () {
                return Date.now()
            }
        }
    });

    let vm2 = new Vue({
        el: '#demo',
        data: {
            firstName: 'Foo',
            lastName: 'Bar'
        },
//        computed: {
//            fullName: function () {
//                return this.firstName + ' ' + this.lastName
//            }
//        }
        computed: {
            fullName: {
                // getter
                get: function () {
                    return this.firstName + ' ' + this.lastName
                },
                // setter
                set: function (newValue) {
                    let names = newValue.split(' ');
                    this.firstName = names[0];
                    this.lastName = names[names.length - 1];
                }
            }
        }
    });

    // 在这个示例中，使用 watch 选项允许我们执行异步操作 (访问一个 API)，
    // 限制我们执行该操作的频率，并在我们得到最终结果前，设置中间状态。这些都是计算属性无法做到的。
    let watchExampleVM = new Vue({
        el: '#watch-example',
        data: {
            question: '',
            answer: 'I cannot give you an answer until you ask a question!'
        },
        watch: {
            // 如果 `question` 发生改变，这个函数就会运行
            question: function (newQuestion) {
                this.answer = 'Waiting for you to stop typing...';
                this.getAnswer()
            }
        },
        methods: {
            // `_.debounce` 是一个通过 Lodash 限制操作频率的函数。
            // 在这个例子中，我们希望限制访问 yesno.wtf/api 的频率
            // AJAX 请求直到用户输入完毕才会发出。想要了解更多关于
            // `_.debounce` 函数 (及其近亲 `_.throttle`) 的知识，
            // 请参考：https://lodash.com/docs#debounce
            getAnswer: _.debounce(
                function () {
                    if (this.question.indexOf('?') === -1) {
                        this.answer = 'Questions usually contain a question mark. ;-)'
                        return
                    }
                    this.answer = 'Thinking...';
                    let vm = this;
                    axios.get('https://yesno.wtf/api')
                        .then(function (response) {
                            vm.answer = _.capitalize(response.data.answer)
                        })
                        .catch(function (error) {
                            vm.answer = 'Error! Could not reach the API. ' + error
                        })
                },
                // 这是我们为判定用户停止输入等待的毫秒数
                500
            )
        }
    })
</script>
</body>
</html>